package cn.edu.jsu.zjj.service.impl;

import cn.edu.jsu.zjj.constant.CommonConstant;
import cn.edu.jsu.zjj.exception.BaseException;
import cn.edu.jsu.zjj.model.dto.ay.AyQueryRequest;
import cn.edu.jsu.zjj.model.entity.Ay;
import cn.edu.jsu.zjj.mapper.AyMapper;
import cn.edu.jsu.zjj.model.utils.SqlUtils;
import cn.edu.jsu.zjj.service.AyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 学年表 服务实现类
 * </p>
 *
 * @author fyt
 * @since 2024-05-12
 */
@Service
public class AyServiceImpl extends ServiceImpl<AyMapper, Ay> implements AyService {
    @Autowired
    private AyMapper ayMapper;

    @Override
    public QueryWrapper<Ay> getQueryWrapper(AyQueryRequest ayQueryRequest) {
        if (ayQueryRequest == null) {
            throw new BaseException("请求参数为空");
        }
        LocalDate createTime = ayQueryRequest.getCreateTime();
        LocalDate endTime = ayQueryRequest.getEndTime();
        String term = ayQueryRequest.getTerm();
        String operName = ayQueryRequest.getOperName();
        String status = ayQueryRequest.getStatus();
        String sortField = ayQueryRequest.getSortField();
        String sortOrder = ayQueryRequest.getSortOrder();
        QueryWrapper<Ay> queryWrapper = new QueryWrapper<>();
        //添加删除条件
        queryWrapper.ne("is_delete", 1);
        queryWrapper.ge(createTime != null, "create_time",createTime);
        queryWrapper.le(endTime != null, "end_time", endTime);
        queryWrapper.eq(StringUtils.isNotBlank(term), "term", term);
        queryWrapper.like(StringUtils.isNotBlank(operName), "oper_name", operName);
        queryWrapper.eq(StringUtils.isNotBlank(status), "status", status);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public boolean getTime(LocalDate createTime, LocalDate endTime) {
        QueryWrapper<Ay> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("end_time", endTime);
        queryWrapper.ge("create_time", createTime);
        return this.count(queryWrapper) <= 0;
    }

    @Override
    public boolean getTerm(String term) {
        QueryWrapper<Ay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("term", term);
        return this.count(queryWrapper) <= 0;
    }

    @Override
    public boolean getTimeSelect(LocalDate createTime, LocalDate endTime) {
        QueryWrapper<Ay> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("end_time", createTime);
        queryWrapper.le("create_time", endTime);
        return this.count(queryWrapper) > 0;
    }

    @Override
    public List<Ay> findAllTerms() {
        QueryWrapper<Ay> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("is_delete", 0);
        List<Ay> terms = ayMapper.selectList(queryWrapper);
        return terms;
    }

}
